﻿using System.Web.Mvc;

namespace Xwltz.Framework.Helper
{
    public static class HtmlHelpers
    {
        public static string IsSelected(this HtmlHelper html, string controller = null, string action = null, string cssClass = null, bool page = false)
        {
            if (string.IsNullOrEmpty(cssClass)) cssClass = "active";

            var currentAction = (string)html.ViewContext.RouteData.Values["action"];
            var currentController = (string)html.ViewContext.RouteData.Values["controller"];

            if (string.IsNullOrEmpty(controller)) controller = currentController;

            if (string.IsNullOrEmpty(action)) action = currentAction;

            var isController = controller.Contains(currentController);
            bool isAction;
            if (cssClass.Equals("in") || cssClass.Equals("active") || cssClass.Equals("layui-this"))
            {
                isAction = action.Contains(currentAction);
            }
            else
            {
                isAction = page ? action.Contains(currentController) : action.Equals("/" + currentController + "/" + currentAction);

                cssClass = "active";
            }
            return isController && isAction ? cssClass : string.Empty;
        }

        public static string PageClass(this HtmlHelper html)
        {
            var currentController = (string)html.ViewContext.RouteData.Values["controller"];
            var currentAction = (string)html.ViewContext.RouteData.Values["action"];
            var currentClass = currentController + " " + currentAction;
            return currentClass;
        }
    }
}
